package com.tool4j.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tool4j.entity.ChatCollection;
import com.tool4j.mapper.ChatCollectionMapper;
import com.tool4j.service.ChatCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class ChatCollectionServiceImpl extends ServiceImpl<ChatCollectionMapper, ChatCollection> implements ChatCollectionService {

    @Autowired
    private ChatCollectionMapper chatCollectionMapper;

    @Override
    public void saveData(ChatCollection params) {
        LambdaQueryWrapper<ChatCollection> query = Wrappers.lambdaQuery(ChatCollection.class)
                .eq(ChatCollection::getUserId, params.getUserId())
                .eq(ChatCollection::getChatId, params.getChatId())
                .last("limit 1");
        ChatCollection exists = chatCollectionMapper.selectOne(query);
        if (exists != null) {
            exists.setContent(params.getContent());
            updateById(exists);
        } else {
            chatCollectionMapper.insert(params);
        }
    }

    @Override
    public List<ChatCollection> findList(ChatCollection params) {
        LambdaQueryWrapper<ChatCollection> query = Wrappers.lambdaQuery(ChatCollection.class)
                .eq(ChatCollection::getUserId, params.getUserId());
        return chatCollectionMapper.selectList(query);
    }
}
